package com.memory.test.controller;


import com.memory.test.comment.api.CommonResult;
import com.memory.test.comment.util.FanyiBaiduUtil;
import com.memory.test.dto.AdminUserDetails;
import com.memory.test.mbg.model.MErrorwords;
import com.memory.test.mbg.model.MUsers;
import com.memory.test.mbg.model.MWords;
import com.memory.test.service.ErrorWordService;
import com.memory.test.service.MUserService;
import com.memory.test.service.WordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Api(value = "words",description = "单词管理")
@Controller
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    @Autowired
    private ErrorWordService errorWordService;

    @Autowired
    private MUserService mUserService;

    @ApiOperation(value = "翻译单词")
    @GetMapping
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult<MWords> translate(@RequestParam String word){
        if(!isChinese(word.charAt(0))){
            word=word.toLowerCase();
        }
        MWords word1=wordService.selectWord(word);

        MErrorwords errorWord=null;


        AdminUserDetails adminUserDetails = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        String username = adminUserDetails.getUsername();
        MUsers user=mUserService.getUserByUsreName(username);

        if(word1==null){
            errorWord=new MErrorwords();
            try {
                if(!isChinese(word.charAt(0))) {
                    String chinese = FanyiBaiduUtil.translateEC(word);
                    word1 = new MWords();
                    word1.setEnglish(word);
                    word1.setLevelId(1L);
                    word1.setChinese(chinese);
                }
                else {
                    String english = FanyiBaiduUtil.translateCE(word);
                    word1 = new MWords();
                    word1.setEnglish(english);
                    word1.setLevelId(1L);
                    word1.setChinese(word);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if(wordService.selectWord(word1.getChinese())==null&&wordService.selectWord(word1.getEnglish())==null){
                wordService.addWord(word1);
                errorWord.setWordId(wordService.selectWord(word).getId());
                errorWord.setCount(1);
                //修改
                errorWord.setUserId(user.getId());

                errorWordService.addWord(errorWord);
            }
            else if(wordService.selectWord(word1.getChinese())==null&&wordService.selectWord(word1.getEnglish())!=null){
                errorWord.setWordId(wordService.selectWord(word1.getEnglish()).getId());
                errorWord.setCount(1);
                //修改
                errorWord.setUserId(user.getId());
                MErrorwords byUserIdAndWordId = errorWordService.findByUserIdAndWordId(errorWord.getUserId(), errorWord.getWordId());
                if(byUserIdAndWordId==null){
                    errorWordService.addWord(errorWord);
                }
                else {
                    errorWord.setCount(errorWord.getCount()+1);
                    errorWordService.updateWord(errorWord);
                }
            }

        }
        else {
            errorWord=new MErrorwords();
            //修改
            MErrorwords word2=errorWordService.findByUserIdAndWordId(user.getId(),word1.getId());
            if(word2==null){
                errorWord=new MErrorwords();
                errorWord.setWordId(word1.getId());
                errorWord.setUserId(user.getId());
                errorWord.setCount(1);
                errorWordService.addWord(errorWord);
            }
            else {
                errorWord.setCount(errorWord.getCount()+1);
                errorWordService.updateWord(errorWord);
            }
        }

        return CommonResult.success(word1);
    }
    public static boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }

        return false;

    }
}
